package com.fangjinwang.zhushunqing.Ui.Home;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.fangjinwang.zhushunqing.Bean.BannerEntity;
import com.fangjinwang.zhushunqing.Bean.HomeEntity;
import com.fangjinwang.zhushunqing.R;
import com.fangjinwang.zhushunqing.Ui.Home.Adapter.ViewPagerAdapter;
import com.fangjinwang.zhushunqing.Ui.Home.Model.HomeModel;
import com.fangjinwang.zhushunqing.Ui.Home.Presenter.HomePresenter;
import com.fangjinwang.zhushunqing.Ui.Home.contrct.HomeContrct;
import com.fangjinwang.zhushunqing.Utils.JsonUtil;
import com.fangjinwang.zhushunqing.Widget.AutoRollViewpager;
import com.fangjinwang.zhushunqing.Widget.DrawableTextTop;
import com.fangjinwang.zhushunqing.Widget.LoadingButton;
import com.jaydenxiao.common.PullRefresh.QQRefreshHeader;
import com.jaydenxiao.common.PullRefresh.RefreshLayout;
import com.jaydenxiao.common.base.BaseFragment;
import com.jaydenxiao.common.commonutils.AndroidUtil;
import com.jaydenxiao.common.commonutils.ImageLoadUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;


/**
 * Created by admin on 2016/11/3.
 */
public class HomeFragment extends BaseFragment<HomePresenter, HomeModel> implements HomeContrct.View ,BGARefreshLayout.BGARefreshLayoutDelegate{
    @Bind(R.id.fragment_home_icon_1)
    DrawableTextTop icon_1;
    @Bind(R.id.fragment_home_icon_2)
    DrawableTextTop icon_2;
    @Bind(R.id.fragment_home_icon_3)
    DrawableTextTop icon_3;
    @Bind(R.id.fragment_home_icon_4)
    DrawableTextTop icon_4;
    @Bind(R.id.btn_qianggou)
    LoadingButton btn;
//    @Bind(R.id.btn_custom)
//    FrameLayout btn_custom;
    @Bind(R.id.fragment_home_viewpager)
    AutoRollViewpager mViewPager;
    @Bind(R.id.fragment_home_banner)
    RelativeLayout mRl;
    @Bind(R.id.fragment_home_viewpager_indicator)
     LinearLayout mIndicatorGroup;
    @Bind(R.id.rl_modulename_refresh)
    RefreshLayout mRefreshLayout;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_home;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }


    @Override
    public void onResume() {
        super.onResume();
        mViewPager.start(4000);
//        FJWApplication.getInstance().getDb().query(FJWApplication.getInstance().get)
    }

    @Override
    protected void initView() {
////        BGARefreshLayout mRefreshLayout = (BGARefreshLayout) findViewById(R.id.rl_modulename_refresh);
//        // 为BGARefreshLayout 设置代理
//        mRefreshLayout.setDelegate(this);
//        // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能
////        BGARefreshViewHolder refreshViewHolder = new XXXImplRefreshViewHolder(this, true);
//        // 设置下拉刷新和上拉加载更多的风格
////        mRefreshLayout.setRefreshViewHolder(refreshViewHolder);
//        BGAMoocStyleRefreshViewHolder holder= new BGAMoocStyleRefreshViewHolder(FJWApplication.getInstance(), false);
//        holder .setOriginalImage(R.mipmap.gesture_node_pressed);
//        holder.setUltimateColor(R.color.blue_check);
//        mRefreshLayout.setRefreshViewHolder(holder);

        mRl.getLayoutParams().height = AndroidUtil.getScreenSize(getActivity(), 1) / 2;
        mRefreshLayout.setRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 延迟3秒后刷新成功
                mRefreshLayout.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mRefreshLayout.refreshComplete();
                    }
                }, 3000);
            }
        });

        QQRefreshHeader header  = new QQRefreshHeader(getActivity());
        mRefreshLayout.setRefreshHeader(header);
        mRefreshLayout.autoRefresh();


        mPresenter.getHomeDataRequest("AppHomeData", null, false, null);
        btn.setLoadingOnClickListener(new LoadingButton.LoadingOnClickListener() {
            @Override
            public void onClick(View v) {
                btn.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        btn.complete("加載失敗，点击重试");
                    }
                },2000);
            }
        });
    }

    @Override
    public void showLoading(String title) {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showErrorTip(String msg) {

    }

    @Override
    public void returnHomeData(String homeEntity) {
        HomeEntity get = JsonUtil.jsonToBean(homeEntity, HomeEntity.class);
        List<BannerEntity> blist = get.getBanner();
        List<View> bannerViewList=new ArrayList<View>();
        List<ImageView> tips=new ArrayList<ImageView>();

        for(int i=0;i<blist.size();i++){
                ImageView imageView = new ImageView(getActivity());

                ImageLoadUtils.displayImage(getContext(), blist.get(i).getImgUrl(), imageView);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                ImageView ivpoint = new ImageView(getActivity());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
                params.leftMargin = 15;
                ivpoint.setLayoutParams(params);
                if (i == 0) {
                    ivpoint.setImageResource(R.mipmap.huipoint);
                } else {
                    ivpoint.setImageResource(R.mipmap.redpoint);
                }
            bannerViewList.add(imageView);
            tips.add(ivpoint);
            mIndicatorGroup.addView(ivpoint);
        }
        ViewPagerAdapter adapter=  new ViewPagerAdapter(getActivity(),bannerViewList);
        mViewPager.setAdapter(adapter);
        mViewPager.setIndicator(mIndicatorGroup,tips);




    }
//    @OnClick(R.id.btn_qianggou)
//    public void btn_qianggou(){
//        ToastUitl.show("--", Toast.LENGTH_SHORT);
////        LoadingBar.show(btn_custom);
//    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }

}
